package com.store.app.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.bean.CouponGoCashBean;
import com.store.app.http.Stores;
import com.store.app.utils.MoneyUtil;

import de.greenrobot.event.EventBus;

/**
 * 余额充值
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public class BalanceRechargeActivity extends BaseActivity {

	private LinearLayout retreat;
	private TextView wallet;
	private EditText et_coupon;//输入金额
	private TextView submit;//确认
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExitApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_balancerecharge);
		initView();
		initData();
		initListeners();
		EventBus.getDefault().register(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	private void initView(){
		retreat= (LinearLayout) findViewById(R.id.retreat);
		wallet = (TextView) findViewById(R.id.wallet);
		et_coupon = (EditText) findViewById(R.id.et_coupon);
		submit= (TextView) findViewById(R.id.submit);
	}
	private void initData(){
		wallet.setText(MoneyUtil.addComma(Stores.cash_balance + ""));
	}
	private void initListeners(){
		retreat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String money = et_coupon.getText().toString();
				if (TextUtils.isEmpty(money)) {
					Toast.makeText(BalanceRechargeActivity.this, "请输入充值金额", Toast.LENGTH_SHORT).show();
					return;
				}
				if(!MoneyUtil.moneyBigThan(money,"0")){
					Toast.makeText(BalanceRechargeActivity.this, "充值金额需大于0", Toast.LENGTH_SHORT).show();
					return;
				}
				money = MoneyUtil.formatMoney(money);
				Intent intent = new Intent(BalanceRechargeActivity.this, BalancePaymentActivity.class);
				intent.putExtra("money", money);
				startActivity(intent);
			}
		});
	}
	public void onEvent(CouponGoCashBean couponGoCashBean){
		if(couponGoCashBean.type==1){
			finish();
		}
	}
}
